package me.dqn.cloudribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author dqn
 * created at 2018/11/8 - 16:05
 */
@RestController
public class MainController {
    @Autowired
    RestTemplate restTemplate;

    @Value("${server.port}")
    String port;

    @GetMapping("/api-hello/{name}")
    public String hello(@PathVariable String name) {
        return getRemoteString(name) + " from [" + port + "]";
    }

    @HystrixCommand(fallbackMethod = "helloError")
    private String getRemoteString(@PathVariable String name) {
        return restTemplate.getForObject("http://CLIENT-HI/hello?name=" + name, String.class);
    }

    private String helloError() {
        return "hello request error.[Hystrix]";
    }

}
